//
//  ST_PeculiaritySquareViewModel.swift
//  btvideo
//
//  Created by mac on 2020/1/14.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SwiftyJSON
import HandyJSON


enum PeculiaritySquareCellType {
    
    case normal
    
    case tab
    /**
     排行
     */
    case rankSection
    /**
     最火
     */
    case hotSection
    /**
     最佳更新特质
     */
    case fantasticUpdateSection
    
    /*
     现在只使用了 normalSection horizontalSection */
    /**
     默认样式
     */
    case normalSection
    
    /**
     水平滑动样式
     */
    case horizontalSection
    
}


class ST_PeculiaritySquareViewModel: NSObject {
    
    var data_array = [ST_PeculiaritySquareCellModel]()
    
    
    var header_array = [ST_PecSquareHeaderViewChildCellModel]()
    
    fileprivate var section_array = [ST_PeculiaritySquareCellModel]()
    
    fileprivate var male_peculiarity_array = [ST_PeculiaritySquareNormalCellModel]()
    
    fileprivate var female_peculiarity_array = [ST_PeculiaritySquareNormalCellModel]()
    
    fileprivate var section_cell_height = 215.cgFloat
    
    fileprivate lazy var normal_cell_size:CGSize = {
       
        let w = ScreenWidth/2.0
        
        return CGSize.init(width: w.int.cgFloat, height: w.int.cgFloat - 8)
    }()
    
    
    var display_sex:ST_SEX{
        get{
//            取反
            if let sex = ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sex,sex == .male{
                return ST_SEX.female
            }
            return ST_SEX.male
        }
    }
    
    
    
    
    
    func create_view_model(json:JSON){
        
        section_array.removeAll()
        
        var genderSub = "女生"
        
        if let myGender = ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sex,myGender == ST_SEX.female{
            genderSub = "男生"
        }
        
        self.header_array = json["recommend_add"].arrayValue.map { (itemJson) -> ST_PecSquareHeaderViewChildCellModel in
            return ST_PecSquareHeaderViewChildCellModel.init(fromJson: itemJson)
        }
        
        
        

        
    //        人气
            if let array = [ST_PeculiarityModel].deserialize(from: json["popularity"].rawString() ?? "") as? [ST_PeculiarityModel]{
                
                let hotSectionModel = ST_PeculiaritySquareCollectionCellModel()
                
                hotSectionModel.title = "人气\(genderSub)特质"
                hotSectionModel.iconImageName = "特质_icon_人气"
                hotSectionModel.array = array
                
                hotSectionModel.cellType = .normalSection
                hotSectionModel.listType = 2
                
                hotSectionModel.cellSize = CGSize.init(width: ScreenWidth, height: section_cell_height)
                
                section_array.append(hotSectionModel)
            }
            

            //      推荐
            if let array = [ST_PeculiarityModel].deserialize(from: json["recommend"].rawString() ?? "") as? [ST_PeculiarityModel]{
                
                let hotSectionModel = ST_PeculiaritySquareCollectionCellModel()
                
                hotSectionModel.title = "推荐\(genderSub)特质"
                hotSectionModel.iconImageName = "特质_icon_推荐"
                
                hotSectionModel.array = array
                
                hotSectionModel.cellType = .normalSection
                hotSectionModel.listType = 3
                
                hotSectionModel.cellSize = CGSize.init(width: ScreenWidth, height: section_cell_height)
                
                section_array.append(hotSectionModel)
            }
            
            
            //      新加
            if let array = [ST_PeculiarityModel].deserialize(from: json["new_feature"].rawString() ?? "") as? [ST_PeculiarityModel]{
                
                let hotSectionModel = ST_PeculiaritySquareCollectionCellModel()
                hotSectionModel.title = "新加\(genderSub)特质"
                hotSectionModel.iconImageName = "特质_icon_新增"
                
                hotSectionModel.array = array
                
                hotSectionModel.cellType = .normalSection
                hotSectionModel.listType = 4
                
                hotSectionModel.cellSize = CGSize.init(width: ScreenWidth, height: section_cell_height)
                
                section_array.append(hotSectionModel)
            }
            
            //最爱
            if let array = [ST_PeculiarityModel].deserialize(from: json["max_love"].rawString() ?? "") as? [ST_PeculiarityModel]{
                
                let hotSectionModel = ST_PeculiaritySquareCollectionCellModel()
                hotSectionModel.title = "\(genderSub)最爱特质"
                hotSectionModel.iconImageName = "特质_icon_女生最爱"
                
                hotSectionModel.array = array
                
                hotSectionModel.cellType = .horizontalSection
                hotSectionModel.listType = 5
                
                hotSectionModel.cellSize = CGSize.init(width: ScreenWidth, height: 320-50)
                
                section_array.append(hotSectionModel)
            }
        
        
        
        let tabModel = ST_PeculiaritySquareTabCellModel()
        
        let item0 = ST_PeculiaritySquareTabCellModel.ST_TabModel.init(str: "女生拥有特质", isSelected: true)
        
        let item1 = ST_PeculiaritySquareTabCellModel.ST_TabModel.init(str: "女生喜欢特质", isSelected: false)
        
        if display_sex == .female{
            tabModel.array = [item0,item1]
        }else{
            item0.isSelected = false
            item1.isSelected = true
            
            item1.str = "男生拥有特质"
            item0.str = "男生喜欢特质"
            tabModel.array = [item1,item0]
        }
       
        tabModel.cellSize = CGSize.init(width: ScreenWidth, height: 82)
        
        
        section_array.append(tabModel)
        
        
        
        
        if let manArray = [ST_PeculiarityModel].deserialize(from: json["man"].rawString() ?? "") as? [ST_PeculiarityModel]{
            
            male_peculiarity_array = manArray.map { (item) -> ST_PeculiaritySquareNormalCellModel in
                
                let m = ST_PeculiaritySquareNormalCellModel()
                
                m.pec = item
                
                m.cellSize = normal_cell_size
                
                return m
            }
        }
        
        if let womanArray = [ST_PeculiarityModel].deserialize(from: json["woman"].rawString() ?? "") as? [ST_PeculiarityModel]{
            
            female_peculiarity_array = womanArray.map { (item) -> ST_PeculiaritySquareNormalCellModel in
                
                let m = ST_PeculiaritySquareNormalCellModel()
                
                m.pec = item
                
                m.cellSize = normal_cell_size
                
                return m
            }
        }
        
        
        
        
        combine_display_model()
    }
    
    func switch_last_array(tag:Int){
        
        guard let tab = section_array.last as? ST_PeculiaritySquareTabCellModel else { return }
        
        if tag == 0{
            
            tab.array[safe:0]?.isSelected = true
            tab.array[safe:1]?.isSelected = false
            
        }else{
            
            tab.array[safe:1]?.isSelected = true
            tab.array[safe:0]?.isSelected = false
            
        }
        
        combine_display_model()
        
    }
    
    fileprivate func combine_display_model(){
        
        var array = section_array
        
        if let tab = section_array.last as? ST_PeculiaritySquareTabCellModel{
            
            if display_sex == .female && tab.array.first?.isSelected == true{
                
                array += female_peculiarity_array
                
            }else if display_sex != .female && tab.array.first?.isSelected == false{
                
                array += female_peculiarity_array
                
            }else{
                
                array += male_peculiarity_array
            }
        }
        
        data_array = array
        
    }
    
    
}

class ST_PeculiaritySquareCellModel:NSObject{
    
    var cellType = PeculiaritySquareCellType.normal
    
    var cellSize = CGSize.zero
    
}


class ST_PeculiaritySquareTabCellModel:ST_PeculiaritySquareCellModel{
    
    override var cellType: PeculiaritySquareCellType{
        get{
            return PeculiaritySquareCellType.tab
        }set{
            super.cellType = newValue
        }
    }
    
    var array = [ST_TabModel]()
    
    
    class ST_TabModel {
        var str = ""
        var isSelected = false
        
        init(str: String, isSelected:Bool) {
            self.str = str
            self.isSelected = isSelected
        }
    }
    
}

/**
 人气     推荐  新加 特质 使用
 */
class ST_PeculiaritySquareCollectionCellModel:ST_PeculiaritySquareCellModel{
    
    var iconImageName = ""
    
    /**
     bug 2700
     */
    var moreDataBackgroundImageUrl = ""
    
    var title = ""
    
    var array = [ST_PeculiarityModel]()
//    类型 1: 推荐新增,2:人气,3:推荐,4:新加特质,5:最爱
    var listType = 1
}


/**
 展示人使用的Section Model
 */
class ST_PeculiaritySquarePersonCellModel:ST_PeculiaritySquareCellModel{
    
    var title = ""
    
    var iconImageName = ""
    
    var moreDataBackgroundImageUrl = ""
    
    var childArray = [ST_PeculiaritySquarePersonChildModel]()
    
}


class ST_PeculiaritySquarePersonChildModel: NSObject,HandyJSON {
    
    var feature_id = 0
    var group_id = 0
    var content = ""
    var video_url = ""
    var remark = ""
    var avatar = ""
    var accid = ""
    var create_time = 0
    
    var width = 100.0
    var height = 100.0
    
    required override init() {
        super.init()
    }
}



class ST_PeculiaritySquareNormalCellModel:ST_PeculiaritySquareCellModel{
    
    override var cellType: PeculiaritySquareCellType{
        get{
            return PeculiaritySquareCellType.normal
        }set{
            super.cellType = newValue
        }
    }
    
    var pec = ST_PeculiarityModel()
    
}





